<?php

namespace Admin\Controller;

class RequirementController extends AdminController {
    
    /*
     * 需求单列表
     */
    public function index(){
        
        $where = " user_id=0 ";
//        $status = I('get.status') == 0 ? 0:1;
//        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        $limit = 8;
        $start = ($page - 1) * $limit;  
        
        $model = M("requirement"); 
        $data = $model->join(" LEFT JOIN user AS u ON u.id = requirement.user_id")->where($where)->order('requirement.id desc')->limit($start,$limit)->field('requirement.* ,u.phone')->select();
        
//        $total = $model->where(array('status' => $status))->count();
        $total = $model->where($where)->count();
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
//        $pagination->url = U('Requirement/index', array('status'=>$status)) . '?page={page}';
        $pagination->url = U('Requirement/index', array('status'=>$status)) . '?page={page}';
        
        $render = $pagination->render();
        $this->assign('nav', 'requirement');
        $this->assign('data', $data);
        $this->assign('render',$render);
        $this->assign('status',$status);
        $this->display('requirement');
        
    }
    
    /*
     * 审核需求单
     */
    public function verify(){
        $result = array(
            'flag' => '0',
            'content' => '操作失败'
        );
        if(IS_POST){
            $data['verify'] = I('post.verify');
            $data['id'] = I('post.id');
            if($data['id'] && $data['verify']>=0){
                $model = M('requirement');
                $flag = $model->save($data);
                if($flag){
                    $result = array(
                        'flag' => '1',
                        'content' => '操作成功'
                    );
                }
            }
        }
        $this->ajaxReturn($result);
    }
    
    /*
     *需求 详情
     */
    
    public function detail(){
        $model = M("requirement"); 
        $data = $model->join(" LEFT JOIN user AS u ON u.id = requirement.user_id")->join("LEFT JOIN user AS t ON t.id = requirement.teacher_id")->join('LEFT JOIN user_teacher AS ut ON ut.user_id = requirement.teacher_id')->where(array('requirement.id'=>(int)I('get.id')))->order('requirement.id desc')->limit(1)->field('requirement.* ,u.phone, t.phone AS t_phone, ut.relname AS t_name')->find();
        $this->assign('data',$data);
        $this->display('detail');
    }
    
    
    
}
